package com.jyw.web.controller.system;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.jyw.common.core.domain.CommonResult;
import com.jyw.common.exception.ServiceException;
import com.jyw.common.utils.DownloadUtil;
import com.jyw.system.domain.vo.SimpleFileVo;
import com.jyw.system.service.ISysFileInfoService;
import com.jyw.system.service.IWeChatAppletService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import it.sauronsoftware.jave.AudioUtils;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * @author Created by ZhangCheng on 2024/01/22
 */
@RestController
@RequestMapping("/system/weChat")
@Api(tags = "微信公众服务")
public class WeChatAppletController {

    @Resource
    private IWeChatAppletService weChatAppletService;

    @Resource
    private ISysFileInfoService sysFileInfoService;

    /**
     * 获取微信JS-JDK签名
     *
     * @param url
     * @return
     */
    @GetMapping("/getSignature")
    @ApiOperation("获取微信JS-JDK签名")
    public CommonResult<WxJsapiSignature> getSignature(String url) {
        if (StrUtil.isNotEmpty(url)) {
            WxJsapiSignature wxJsapiSignature = weChatAppletService.getSign(url);
            return CommonResult.success(wxJsapiSignature);
        } else {
            return CommonResult.error("url地址不能为空");
        }
    }

    /**
     * 上传文件
     *
     * @param file
     * @param mediaType
     * @return
     */
    @PostMapping(value = "/uploadFile")
    @ApiOperation(value = "上传文件")
    @ApiImplicitParam(name = "file", value = "文件",  paramType = "formData",required = true,dataType = "file")
    public CommonResult<WxMediaUploadResult> uploadFile(@RequestPart("file") MultipartFile file,String mediaType){
        if (null == file) {
            throw new ServiceException("上传文件不能为空");
        }
        WxMediaUploadResult result=weChatAppletService.uploadFile(file,mediaType);
        return CommonResult.success(result);
    }

    /**
     * 获取下载文件
     *
     * @param mediaId
     * @param response
     */
    @GetMapping(value = "/getFile")
    @ApiOperation(value = "获取下载文件")
    public void getFile(String mediaId, HttpServletResponse response) {
        File file = weChatAppletService.getFileByMediaId(mediaId);
        DownloadUtil.download(file, response);
    }

    /**
     * 获取临时录音文件
     *
     * @param mediaId
     * @param response
     */
    @GetMapping(value = "/getSoundFile")
    @ApiOperation(value = "获取临时录音文件")
    public void getSoundFile(String mediaId, HttpServletResponse response) {
        File file = weChatAppletService.getFileByMediaId(mediaId);
        DownloadUtil.download(file, response);
    }




    /**
     * 上传文件
     *
     * @param mediaId
     */
    @GetMapping(value = "/minio/uploadFile")
    @ApiOperation(value = "上传微信文件")
    public CommonResult<SimpleFileVo> uploadFile(String mediaId) {
        File file = weChatAppletService.getFileByMediaId(mediaId);
        try {
            return CommonResult.success(sysFileInfoService.upload(file));
        } catch (Exception e) {
            throw new ServiceException("文件上传失败");
        }
    }

    /**
     * 上传录音文件
     *
     * @param mediaId
     */
    @GetMapping(value = "/minio/uploadSoundFile")
    @ApiOperation(value = "上传微信录音文件")
    public CommonResult<String> uploadSoundFile(String mediaId) {
        File file = weChatAppletService.getFileByMediaId(mediaId);
        File tempFile = null;
//        AudioUtils.amrToMp3();
        try {
            //声明临时文件
            tempFile = File.createTempFile(UUID.fastUUID().toString(true),".mp3");
            //转换MP3
            AudioUtils.amrToMp3(file,tempFile);
            return CommonResult.success("上传成功",sysFileInfoService.uploadReturnUrl(tempFile));
        } catch (Exception e) {
            throw new ServiceException("文件上传失败");
        }finally {
            if(!ObjectUtils.isEmpty(tempFile)){
                tempFile.delete();
            }
        }
    }
}
